package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IDealInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IEnrollTeamMemberService;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 续报成交数据 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-06-17
 */
@Service
public class DealInfoServiceImpl extends ServiceImpl<DealInfoMapper, DealInfo> implements IDealInfoService {


    @Autowired
    StaffMapper staffMapper;

    @Autowired
    @Lazy
    OrderApplyMapper orderApplyMapper;

    @Autowired
    @Lazy
    OrderDetailMapper orderDetailMapper;

    @Autowired
    @Lazy
    IEnrollTeamMemberService iEnrollTeamMemberService;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    @Lazy
    SubjectsMapper subjectsMapper;

    @Autowired
    @Lazy
    OrganizationMapper organizationMapper;

    @Autowired
    @Lazy
    StudentGradeMapper studentGradeMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    IdWorker idWorker;

    public boolean addDealInfo(OrderApply orderApplyInfo){

        OrderApply orderApply = orderApplyMapper.selectById(orderApplyInfo.getId());
        List<Long> dealList = orderApplyInfo.getDealList();

        if(orderApply == null){
            throw new CommonException(ResultCode.NOORDER);
        }
        OrderDetail orderDetail = orderDetailMapper.selectOne(new QueryWrapper<OrderDetail>().eq("order_id",orderApply.getId()).last("limit 1"));
        Integer dealCount = (Integer) orderDetail.getAllCourseCount()/15;
        if(dealList!=null){
            //Integer dealListCount = dealList.size();
            //BigDecimal dealNum = new BigDecimal(dealCount).divide(new BigDecimal(dealListCount),2, RoundingMode.HALF_UP);
            for (Long id:dealList){
                DealInfo dealInfo = new DealInfo();
                BeanUtils.copyProperties(orderDetail,dealInfo);
                dealInfo.setId(idWorker.nextId());
                //dealInfo.setCount(dealNum);
                dealInfo.setOrderId(orderApply.getId());
                dealInfo.setSchoolId(orderApply.getSchoolId());
                dealInfo.setSubjectsId(orderDetail.getSubjectsId());
                dealInfo.setTeacherId(id);
                dealInfo.setAddDate(LocalDate.now());
                this.save(dealInfo);
            }
        }

        this.updateCount(orderApplyInfo.getId());
        return true;
    }

    public boolean updateCount(Long orderId){
        OrderApply orderApply = orderApplyMapper.selectById(orderId);
        OrderDetail orderDetail = orderDetailMapper.selectOne(new QueryWrapper<OrderDetail>().eq("order_id",orderApply.getId()).last("limit 1"));
        Integer dealCount = (Integer) orderDetail.getAllCourseCount()/45;

        QueryWrapper<DealInfo> dealInfoQueryWrapper = new QueryWrapper<>();
        dealInfoQueryWrapper.eq("order_id",orderId);
        List<DealInfo> dealInfoList = this.list(dealInfoQueryWrapper);
        Integer dealListCount = dealInfoList.size();
        if (dealListCount>0){
            BigDecimal dealNum = new BigDecimal(dealCount).divide(new BigDecimal(dealListCount),2, RoundingMode.HALF_UP);
            for (DealInfo dealInfo:dealInfoList){
                dealInfo.setCount(dealNum);
                this.updateById(dealInfo);
            }
        }
        return true;
    }

    public boolean delDealInfo(Long id){
        DealInfo dealInfo = this.getById(id);
        if(dealInfo!=null){
            this.removeById(dealInfo.getId());
            this.updateCount(dealInfo.getOrderId());
        }
        return true;
    }
}
